#include <SPI.h>


#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

#define SEGMENT_COUNT 4

const int ledPin = 7; 
const int buttonPin = 2;
const float unit = 0.05;

const char* morseCode[] = {
    ".-",   "-...", "-.-.", "-..",  ".",    "..-.", "--.",  "....", 
    "..",   ".---", "-.-",  ".-..", "--",   "-.",   "---",  ".--.", 
    "--.-", ".-.",  "...",  "-",    "..-",  "...-", ".--",  "-..-", 
    "-.--", "--.."
};
const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


const byte charMap[26][8] = {
  {0b00011000, 0b00100100, 0b00100100, 0b00111100, 0b00100100, 0b00100100, 0b00100100, 0b00000000}, // A
  {0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00000000}, // B
  {0b00011100, 0b00100010, 0b00100000, 0b00100000, 0b00100000, 0b00100010, 0b00011100, 0b00000000}, // C
  {0b00111100, 0b00100100, 0b00100100, 0b00100100, 0b00100100, 0b00100100, 0b00111100, 0b00000000}, // D
  {0b00111100, 0b00100000, 0b00100000, 0b00111100, 0b00100000, 0b00100000, 0b00111100, 0b00000000}, // E
  {0b00111100, 0b00100000, 0b00100000, 0b00111100, 0b00100000, 0b00100000, 0b00100000, 0b00000000}, // F
  {0b00011100, 0b00100010, 0b00100000, 0b00101110, 0b00100010, 0b00100010, 0b00011100, 0b00000000}, // G
  {0b00100100, 0b00100100, 0b00100100, 0b00111100, 0b00100100, 0b00100100, 0b00100100, 0b00000000}, // H
  {0b00011100, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00011100, 0b00000000}, // I
  {0b00001110, 0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b00100100, 0b00011000, 0b00000000}, // J
  {0b00100100, 0b00101000, 0b00110000, 0b00111000, 0b00101000, 0b00100100, 0b00100010, 0b00000000}, // K
  {0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00111100, 0b00000000}, // L
  {0b00100010, 0b00110110, 0b00101010, 0b00101010, 0b00100010, 0b00100010, 0b00100010, 0b00000000}, // M
  {0b00100010, 0b00100010, 0b00110010, 0b00101010, 0b00100110, 0b00100010, 0b00100010, 0b00000000}, // N
  {0b00011100, 0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00011100, 0b00000000}, // O
  {0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00100000, 0b00100000, 0b00100000, 0b00000000}, // P
  {0b00011100, 0b00100010, 0b00100010, 0b00100010, 0b00101010, 0b00100110, 0b00011110, 0b00000000}, // Q
  {0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00101000, 0b00100100, 0b00100010, 0b00000000}, // R
  {0b00011100, 0b00100010, 0b00100000, 0b00011100, 0b00000010, 0b00100010, 0b00011100, 0b00000000}, // S
  {0b00111100, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00000000}, // T
  {0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00011100, 0b00000000}, // U
  {0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00100010, 0b00010100, 0b00001000, 0b00000000}, // V
  {0b00100010, 0b00100010, 0b00100010, 0b00101010, 0b00101010, 0b00101010, 0b00010100, 0b00000000}, // W
  {0b00100010, 0b00100010, 0b00010100, 0b00001000, 0b00010100, 0b00100010, 0b00100010, 0b00000000}, // X
  {0b00100010, 0b00100010, 0b00100010, 0b00010100, 0b00001000, 0b00001000, 0b00001000, 0b00000000}, // Y
  {0b00111110, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b00111110, 0b00000000}  // Z
};

// Function to convert text to Morse code
String textToMorse(const String& text) {
    String morse = "";
    for (size_t i = 0; i < text.length(); i++) {
        char c = toupper(text[i]);
        if (c == ' ') {
            morse += " "; // Space between words
        } else {
            int index = strchr(letters, c) - letters;
            if (index >= 0 && index < 26) {
                morse += morseCode[index];
                morse += " "; // Space between characters
            }
        }
    }
    return morse;
}

// Function to blink Morse code
void blinkMorse(const String& morse) {
    for (size_t i = 0; i < morse.length(); i++) {
        char c = morse[i];
        if (c == '.') {
            digitalWrite(ledPin, HIGH);
            delay(unit * 1000); // Dot duration
        } else if (c == '-') {
            digitalWrite(ledPin, HIGH);
            delay(3 * unit * 1000); // Dash duration
        } else if (c == ' ') {
            delay(7 * unit * 1000); // Word gap
            continue;
        }
        digitalWrite(ledPin, LOW);
        delay(unit * 1000); // Gap between elements
    }
}




const char* words[] = {"MAY", "YOU", "GET", "RICH"};
const int wordCount = 4;

void sendAll(int registerIndex, int value) {
  digitalWrite(CS_PIN, LOW);
  for (int i = 0; i < SEGMENT_COUNT; i++) {
    SPI.transfer(registerIndex);
    SPI.transfer(value);
  }
  digitalWrite(CS_PIN, HIGH);
}

void clearDisplays() {
  for (int row = 1; row <= 8; row++) {
    sendAll(row, 0);
  }
}

void setup() {
  pinMode(ledPin, OUTPUT); // Set LED pin as output
  digitalWrite(ledPin, LOW); // Ensure LED is off
  String text = "MAY YOU GET RICH";
    String morse = textToMorse(text);

    // Blink the message
    blinkMorse(morse);
  SPI.begin();
  pinMode(CS_PIN, OUTPUT);
  sendAll(0xf, 0); // Disable test mode
  sendAll(0xb, 7); // Set scanlines to 8
  clearDisplays();
  sendAll(0xc, 1); // Enable display
}

void displayWord(const char* word) {
  clearDisplays();

  for (int row = 1; row <= 8; row++) {
    digitalWrite(CS_PIN, LOW);
    for (int segment = 0; segment < SEGMENT_COUNT; segment++) {
      char c = word[segment];
      byte rowData = 0;

      if (c != '\0' && c >= 'A' && c <= 'Z') {
        int charIndex = c - 'A';
        rowData = charMap[charIndex][row - 1];
      }

      SPI.transfer(row);
      SPI.transfer(rowData);
    }
    digitalWrite(CS_PIN, HIGH);
  }
}

void loop() {
  for (int i = 0; i < wordCount; i++) {
    displayWord(words[i]);
    delay(2000); // Flash each word for 2 seconds
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led1:A
led1:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2
matrix1:V+
matrix1:GND
matrix1:DIN
matrix1:CS
matrix1:CLK
matrix1:V+.2
matrix1:GND.2
matrix1:DOUT
matrix1:CS.2
matrix1:CLK.2